package com.grant.code.utils;

import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.image.Image;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import javafx.scene.paint.Color;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * UI工具类 - 提供公共UI相关方法
 * 包括对话框、图标加载和样式应用等
 */
public class UIUtils {

    // 图片资源缓存
    private static final Map<String, Image> imageCache = new HashMap<>();
    
    /**
     * 显示信息对话框
     * @param title 标题
     * @param content 内容
     */
    public static void showInfoDialog(String title, String content) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle(title);
        alert.setHeaderText(null);
        alert.setContentText(content);
        alert.showAndWait();
    }
    
    /**
     * 显示信息对话框(带标题和头部)
     * @param title 标题
     * @param header 头部文本
     * @param content 内容
     */
    public static void showInformationAlert(String title, String header, String content) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle(title);
        alert.setHeaderText(header);
        alert.setContentText(content);
        alert.showAndWait();
    }
    
    /**
     * 显示错误对话框
     * @param title 标题
     * @param content 内容
     */
    public static void showErrorDialog(String title, String content) {
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setTitle(title);
        alert.setHeaderText(null);
        alert.setContentText(content);
        alert.showAndWait();
    }
    
    /**
     * 显示错误对话框(带标题和头部)
     * @param title 标题
     * @param header 头部文本
     * @param content
     */
    public static void showErrorAlert(String title, String header, String content) {
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setTitle(title);
        alert.setHeaderText(header);
        alert.setContentText(content);
        alert.showAndWait();
    }
    
    /**
     * 显示警告对话框
     * @param title 标题
     * @param content 内容
     */
    public static void showWarningDialog(String title, String content) {
        Alert alert = new Alert(Alert.AlertType.WARNING);
        alert.setTitle(title);
        alert.setHeaderText(null);
        alert.setContentText(content);
        alert.showAndWait();
    }
    
    /**
     * 显示警告对话框(带标题和头部)
     * @param title 标题
     * @param header 头部文本
     * @param content 内容
     */
    public static void showWarningAlert(String title, String header, String content) {
        Alert alert = new Alert(Alert.AlertType.WARNING);
        alert.setTitle(title);
        alert.setHeaderText(header);
        alert.setContentText(content);
        alert.showAndWait();
    }
    
    /**
     * 显示确认对话框
     * @param title 标题
     * @param content 内容
     * @return 用户是否确认
     */
    public static boolean showConfirmDialog(String title, String content) {
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setTitle(title);
        alert.setHeaderText(null);
        alert.setContentText(content);
        Optional<ButtonType> result = alert.showAndWait();
        return result.isPresent() && result.get() == ButtonType.OK;
    }
    
    /**
     * 显示带有自定义头部的确认对话框
     * @param title 标题
     * @param header 头部文本
     * @param content 内容
     * @return 用户是否确认
     */
    public static boolean showConfirmDialog(String title, String header, String content) {
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setTitle(title);
        alert.setHeaderText(header);
        alert.setContentText(content);
        Optional<ButtonType> result = alert.showAndWait();
        return result.isPresent() && result.get() == ButtonType.OK;
    }
    
    /**
     * 显示确认对话框(另一种方法名)
     * @param title 标题
     * @param header 头部文本
     * @param content 内容
     * @return 用户是否确认
     */
    public static boolean showConfirmationAlert(String title, String header, String content) {
        return showConfirmDialog(title, header, content);
    }
    
    /**
     * 从资源中加载图片
     * @param path 资源路径
     * @return 图片对象
     */
    public static Image loadImageResource(String path) {
        return imageCache.computeIfAbsent(path, p -> {
            try (InputStream is = UIUtils.class.getResourceAsStream(p)) {
                if (is == null) {
                    System.err.println("资源未找到: " + p);
                    return createPlaceholderImage();
                }
                return new Image(is);
            } catch (Exception e) {
                System.err.println("加载图片失败: " + p + ", 错误: " + e.getMessage());
                return createPlaceholderImage();
            }
        });
    }
    
    /**
     * 创建占位图
     * @return 占位图对象
     */
    public static Image createPlaceholderImage() {
        WritableImage img = new WritableImage(24, 24);
        PixelWriter pixelWriter = img.getPixelWriter();
        
        for (int y = 0; y < 24; y++) {
            for (int x = 0; x < 24; x++) {
                pixelWriter.setColor(x, y, (x + y) % 2 == 0 ? Color.LIGHTGRAY : Color.DARKGRAY);
            }
        }
        
        return img;
    }
    
    /**
     * 创建视频占位图
     * @return 视频占位图对象
     */
    public static Image createVideoPlaceholderImage() {
        try {
            // 尝试从资源文件加载视频占位图标
            String resourcePath = "/images/video-placeholder.png";
            return loadImageResource(resourcePath);
        } catch (Exception e) {
            // 如果加载失败，创建一个简单的彩色占位图
            WritableImage placeholderImage = new WritableImage(150, 100);
            PixelWriter pixelWriter = placeholderImage.getPixelWriter();
            
            // 使用深灰色背景
            Color bgColor = Color.DARKGRAY;
            
            // 填充背景色
            for (int y = 0; y < 100; y++) {
                for (int x = 0; x < 150; x++) {
                    pixelWriter.setColor(x, y, bgColor);
                }
            }
            
            // 在中间绘制一个播放图标
            Color iconColor = Color.WHITE;
            int centerX = 75;
            int centerY = 50;
            int triangleSize = 30;
            
            for (int y = centerY - triangleSize/2; y <= centerY + triangleSize/2; y++) {
                for (int x = centerX - triangleSize/4; x <= centerX + triangleSize/2; x++) {
                    // 创建一个简单的三角形播放图标
                    if (x >= centerX && 
                        y >= centerY - (x - centerX) && 
                        y <= centerY + (x - centerX)) {
                        pixelWriter.setColor(x, y, iconColor);
                    }
                }
            }
            
            return placeholderImage;
        }
    }
    
    /**
     * 格式化时间
     * @param millis 毫秒时间
     * @return 格式化后的时间字符串 (mm:ss)
     */
    public static String formatTime(double millis) {
        millis /= 1000; // 将毫秒转换为秒
        String sec = String.format("%02d", (int)(millis % 60)); // 格式化秒数
        String min = String.format("%02d", (int)((millis / 60) % 60)); // 格式化分钟数
        return min + ":" + sec; // 返回格式化后的时间字符串
    }
    
    /**
     * 格式化文件大小
     * @param bytes 文件大小（字节）
     * @return 格式化后的文件大小字符串
     */
    public static String formatFileSize(long bytes) {
        if (bytes < 1024) {
            return bytes + " B";
        } else if (bytes < 1024 * 1024) {
            return String.format("%.2f KB", bytes / 1024.0);
        } else if (bytes < 1024 * 1024 * 1024) {
            return String.format("%.2f MB", bytes / (1024.0 * 1024.0));
        } else {
            return String.format("%.2f GB", bytes / (1024.0 * 1024.0 * 1024.0));
        }
    }
} 